﻿/*
Copyright (C) 2012 Laurent Tardif (laurent.tardif@persistentsas.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using org.codeplex.TestUtils;


namespace IQToolsFonctionalTests
{
    [Binding]
    public class StepDefinitionValidateDiff
    {

        String file1 = null;
        String file2 = null;
        String generatedFile = null;
        String referenceFile = null;
        String outputFile = null;

        [Given(@"I have a server with powershell installed")]
        public void GivenIHaveAServerWithPorwershellInstalled()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0");
            Assert.IsTrue(dirInfo.Exists);
            FileInfo fileInfo = new FileInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe");
            Assert.IsTrue(fileInfo.Exists);
            //TODO : check file version > 1.0 ? 

        }


        [Given(@"I have provided a first file named (.*) with valid data")]
        public void GivenIHaveProvidedAFirstFileNamedOut1_XmlWithValidData(String name)
        {
            String dir = Directory.GetCurrentDirectory();
            file1 = Path.Combine(dir, name);
            Console.WriteLine("Will work with file " + file1);
        }

        [Given(@"I have provided a second file named (.*) with valid data")]
        public void GivenIHaveProvidedASecondFileNamedOut1_XmlWithValidData(String name)
        {
            String dir = Directory.GetCurrentDirectory();
            file2 = Path.Combine(dir, name);
            Console.WriteLine("Will work with the second file " + file2);
        }

        [Given(@"the output file name provided is (.*)")]
        public void GivenTheOutputFileNameProvided(String outputName)
        {
            String dir = Directory.GetCurrentDirectory();
            outputFile = Path.Combine(dir, outputName);
            Console.WriteLine("Will work with result file " + outputFile);
            Assert.IsFalse(File.Exists(outputFile), "the file "+outputFile+" should not exist");
        }

        [Then(@"the result (.*) should be equals to the reference file (.*)")]
        public void ThenTheResult_ShouldBeEqualsToTheReferenceFile(String pGeneratedFile, String pReferenceFile)
        {
            String dir = Directory.GetCurrentDirectory();
            generatedFile = Path.Combine(dir, pGeneratedFile);
            referenceFile = Path.Combine(dir, pReferenceFile);
            Console.WriteLine("Will work with result file (" + generatedFile+")");
            Console.WriteLine("Will compare it with reference file (" + referenceFile+")");
            Assert.IsTrue(File.Exists(generatedFile), "the file (" + generatedFile + ") should exist");
            Assert.IsTrue(File.Exists(referenceFile), "the file (" + referenceFile + ") should exist");
            //check file arze equals
            Boolean res = FileUtils.checkFileAreEquals(generatedFile, referenceFile);
            Assert.IsTrue(res, "File are not equals");
        }

        [Then(@"the result file (.*) should be removed")]
        public void ThenTheResultFile_ShouldBeRemoved(String outputName)
        {
            String dir = Directory.GetCurrentDirectory();
            outputFile = Path.Combine(dir, outputName);
            Console.WriteLine("Will work with result file " + outputFile);
            Assert.IsTrue(File.Exists(outputFile), "the file " + outputFile + " should exist");
            File.Delete(outputFile);
            Assert.IsFalse(File.Exists(outputFile), "the file " + outputFile + " should not exist");
        }

        [Then(@"the result (.*) should not contain any difference")]
        public void ThenTheResult_ShouldNotContainsAnyDifference(String pGeneratedFile)
        {
            String dir = Directory.GetCurrentDirectory();
            generatedFile = Path.Combine(dir, pGeneratedFile);
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
            String content = File.ReadAllText(pGeneratedFile);
            Boolean check =  StringUtils.CheckDoNotContains(content, "<add");
            Assert.IsTrue(check, "File contains add");
            check = StringUtils.CheckDoNotContains(content, "<miss");
            Assert.IsTrue(check, "File contains miss");
            check = StringUtils.CheckDoNotContains(content, "<diff");
            Assert.IsTrue(check, "File contains diff");
  
        }

        [When(@"I run the script in simple mode")]
        public void WhenIRunTheScriptInSimpleMode()
        {
            Console.WriteLine("start success");
            int errorCode = ShellUtils.ExecutePowershellScript32("IQDiff.ps1", @"-file1 " + file1 + " -file2 " + file2 + " -output " + outputFile + " -simple", 10000);
            Assert.AreEqual(0, errorCode, "The return code should be = 0 as all parameters are valid (directory)");
            Console.WriteLine("run success");
        }

        [When(@"I run the script")]
        public void WhenIRunTheScript()
        {
            Console.WriteLine("start success");
            int errorCode = ShellUtils.ExecutePowershellScript32("IQDiff.ps1", @"-file1 " + file1 + " -file2 " + file2 + " -output " + outputFile+" ", 10000);
            Assert.AreEqual(0, errorCode, "The return code should be = 0 as all parameters are valid (directory)");
            Console.WriteLine("run success");
        }

        [Then(@"the diff resulting file should not contain a (.*) section")]
        public void ThenTheResultingFileShouldNotContainsASoftwareSection(string sectionName)
        {
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
            String content = File.ReadAllText(generatedFile);
            //match softwares and softaware 
            Boolean check = StringUtils.CheckDoNotContains(content, "<" + sectionName);
            Assert.IsTrue(check, "File contains a " + sectionName + " section");
        }

        [Then(@"the result (.*) should not contain a (.*) section")]
        public void ThenTheResultFileShouldNotContainsAGivenSection(string outputName, string sectionName)
        {
            String dir = Directory.GetCurrentDirectory();
            outputFile = Path.Combine(dir, outputName);
            Console.WriteLine("Will work with result file " + outputFile);
            Assert.IsTrue(File.Exists(outputFile), "the file " + outputFile + " should exist");

            String content = File.ReadAllText(outputFile);
            //match softwares and softaware 
            Boolean check = StringUtils.CheckDoNotContains(content, "<" + sectionName);
            Assert.IsTrue(check, "File contains a " + sectionName + " section");
        }

        [Then(@"the result (.*) should contain a (.*) section")]
        public void ThenTheResultFileShouldContainsAGivenSection(string outputName, string sectionName)
        {
            String dir = Directory.GetCurrentDirectory();
            outputFile = Path.Combine(dir, outputName);
            Console.WriteLine("Will work with result file " + outputFile);
            Assert.IsTrue(File.Exists(outputFile), "the file " + outputFile + " should exist");

            String content = File.ReadAllText(outputFile);
            //match softwares and softaware 
            Boolean check = StringUtils.CheckContains(content, "<" + sectionName);
            Assert.IsTrue(check, "File contains a " + sectionName + " section");
        }
    }

}
